//
//  TodayInHistoryTableViewCell.m
//  LifeTools
//
//  Created by alex on 19/10/2016.
//  Copyright © 2016 chinaclick. All rights reserved.
//

#import "TodayInHistoryTableViewCell.h"

@interface TodayInHistoryTableViewCell ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *desLabel;

@end

@implementation TodayInHistoryTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier model:(TodayInHistroyDataModel *)model {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSString *platfrom = [[[[UIDevice currentDevice] machine] substringToIndex:9] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        
        _imgView = [[UIImageView alloc] init];
        _imgView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imgView];
        
        [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).with.offset(ScreenWidth * 0.02);
            make.top.equalTo(self.mas_top).with.offset(ScreenWidth * 0.02);
            make.width.height.mas_equalTo(ScreenWidth * 0.2);
        }];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 0;
        _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        CGSize titleSize = CGSizeMake(ScreenWidth * 0.74, MAXFLOAT);
        CGSize contentTitleSize = [model.title boundingRectWithSize:titleSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
        int titleScale = 0;
        
        titleScale = (int)contentTitleSize.height / 16;
        [self addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_imgView.mas_right).with.offset(ScreenWidth * 0.02);
            make.right.equalTo(self.mas_right).with.offset(- ScreenWidth * 0.02);
            make.top.equalTo(self.mas_top).with.offset(ScreenWidth * 0.02);
            if ([platfrom isEqualToString:@"iPhone 4"] || [platfrom isEqualToString:@"iPhone 4S"]) {
                make.height.mas_equalTo(ScreenWidth * 0.05 * (titleScale + 1));
            } else {
                make.height.mas_equalTo(ScreenWidth * 0.05 * titleScale);
            }
        }];
        
        _desLabel = [[UILabel alloc] init];
        _desLabel.font = [UIFont systemFontOfSize:13];
        _desLabel.textAlignment = NSTextAlignmentLeft;
        _desLabel.numberOfLines = 0;
        _desLabel.lineBreakMode = NSLineBreakByWordWrapping;
        CGSize size = CGSizeMake(ScreenWidth * 0.74, MAXFLOAT);
        CGSize contentSize = [model.des boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
        int scale = 0;

        scale = (int)contentSize.height / 16;
        [self addSubview:_desLabel];
        
        [_desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_imgView.mas_right).with.offset(ScreenWidth * 0.02);
            make.right.equalTo(self.mas_right).with.offset(- ScreenWidth * 0.02);
            make.top.equalTo(_titleLabel.mas_bottom).with.offset(ScreenWidth * 0.02);
            if ([platfrom isEqualToString:@"iPhone 4"] || [platfrom isEqualToString:@"iPhone 4S"]) {
                make.height.mas_equalTo(ScreenWidth * 0.05 * (scale + 1));
            } else {
                make.height.mas_equalTo(ScreenWidth * 0.05 * scale);
            }
        }];
    }
    return self;
}

- (void)reloadData:(TodayInHistroyDataModel *)model {
    UIImage *placeholder = [UIImage imageNamed:@"imgNotExist"];
    UIImage *originalImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.picUrl];
    if (originalImage) {
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:placeholder];
    } else {
        AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
        NSLog(@"%s,%d",__FUNCTION__,mgr.isReachableViaWiFi);
        NSLog(@"model.picUrl-==%@",model.picUrl);

        if (mgr.isReachableViaWiFi) { //在wifi状态下，下载原图
            [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:placeholder];
        } else if (mgr.isReachableViaWWAN) { // 在使用手机自带网络
            [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:placeholder];
        } else { // 没有网络
            if (originalImage) { // 内存\沙盒缓存中有小图
                [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:placeholder];
            } else {
                [self.imgView sd_setImageWithURL:nil placeholderImage:placeholder];
            }
        }
    }
    
    self.titleLabel.text = model.title;
    self.desLabel.text = model.des;
}
@end
